package com.privatetime.web;

import java.util.List;
import java.util.Set;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ModelDriven;
import com.privatetime.model.Group;
import com.privatetime.model.User;

public class GroupAction extends BaseAction implements ModelDriven<Group> {

	private static final long serialVersionUID = 1L;

	private Group group = new Group();
	private List<Group> checkboxList;
	private Set<Group> currentUserGroups;
	private Integer userId;

	public List<Group> getCheckboxList() {
		return checkboxList;
	}

	@Override
	public String create() throws Exception {
		logger.info("New Group is " + group);
		Integer id = groupService.createEntity(group);
		logger.info("New Gourp Id:" + id);
		return ActionStateConstants.SUCCESS;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	@SkipValidation
	public String list() {
		checkboxList = groupService.findEntityAll();
		return ActionStateConstants.SUCCESS;
	}

	@Override
	public String retrieve() {
		return null;
	}

	@Override
	public String update() throws Exception {
		return null;
	}

	@SkipValidation
	public String checkboxList() throws Exception {
		checkboxList = groupService.findEntityAll();
		// HttpServletRequest request = ServletActionContext.getRequest();
		// Integer userId = Integer.valueOf(request.getParameter("id"));
		// User currentUser = userService.retrieveEntity(userId);
		logger.info("--------------" + userId);
		User currentUser = userService.retrieveEntity(userId);
		currentUserGroups = currentUser.getGroupSet();
		return ActionStateConstants.SUCCESS;
	}

	public Group getModel() {
		return group;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public Set<Group> getCurrentUserGroups() {
		return currentUserGroups;
	}

	@Override
	public void validate() {
		if (group.getGroupName() == null || group.getGroupName().equals("")) {
			addFieldError("groupname", "groupname can not null");
		}
	}
}
